﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class VisiBoardStatusModel : ComparableBaseModel<VisiBoardStatusModel>, IDataErrorInfo
    {
        public enum VisiCentralGroupEnum
        {
            Undefined = 0,
            One = 1,
            Two = 2,
            Three = 3
        }

        private int _cellId;
        public int CellId
        {
            get { return _cellId; }
            set
            {
                _cellId = value;
                OnPropertyChanged("CellId");
            }
        }

        private int _areaId;
        public int AreaId
        {
            get { return _areaId; }
            set
            {
                _areaId = value;
                OnPropertyChanged("AreaId");
            }
        }

        public bool IsSaveEnabled
        {
            get
            {
                return IsChange && !string.IsNullOrEmpty(_cellName) && !string.IsNullOrEmpty(_description);
            }
        }

        private string _cellName;
        [Category("1. VisiBoard")]
        [DisplayName("Name")]
        [Description("Cell Name.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string CellName
        {
            get { return _cellName; }
            set
            {
                _cellName = value;
                OnPropertyChanged("CellName", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _description;
        [Category("1. Properties")]
        [DisplayName("Description")]
        [Description("Cell description.")]
        [DefaultValue("")]
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _password;
        [Category("1. Properties")]
        [DisplayName("Password")]
        [Description("Cell password.")]
        [DefaultValue("")]
        public string Password
        {
            get { return _password; }
            set
            {
                if (_password == value) return;
                _password = value;
                OnPropertyChanged("Password", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private bool _enabled;
        [Category("1. Properties")]
        [DisplayName("Enabled")]
        [Description("Enabled / Disabled this cell.")]
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                _enabled = value;
                OnPropertyChanged("Enabled", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        VisiCentralGroupEnum _visicentral_group;
        byte _visicentral_row;
        byte _visicentral_column;

        [Category("2. VisiCentral")]
        [DisplayName("Group")]
        [Description("VisiCentral Group that this cell is in.")]
        public VisiCentralGroupEnum VisiCentralGroup
        {
            get { return _visicentral_group; }
            set
            {
                _visicentral_group = value;
                OnPropertyChanged("VisiCentralGroup", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        [Category("2. VisiCentral")]
        [DisplayName("Group Row")]
        [Description("Row within VisiCentral grouping.")]
        public byte VisiCentralRow
        {
            get { return _visicentral_row; }
            set
            {
                _visicentral_row = value;
                OnPropertyChanged("VisiCentralRow", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        [Category("2. VisiCentral")]
        [DisplayName("Group Column")]
        [Description("Column within VisiCentral grouping.")]
        public byte VisiCentralColumn
        {
            get { return _visicentral_column; }
            set
            {
                _visicentral_column = value;
                OnPropertyChanged("VisiCentralRow", true);
                OnPropertyChanged("IsSaveEnabled");
            }
        }

        private string _createdBy;
        [Category("3. Update History")]
        [DisplayName("Created By")]
        [Description("User who created the cell in VisiAdmin.")]
        [ReadOnly(true)]
        public string CreatedBy
        {
            get { return _createdBy; }
            set
            {
                _createdBy = value;
                OnPropertyChanged("CreatedBy");
            }
        }

        private string _createdDate;
        [Category("3. Update History")]
        [DisplayName("Created On")]
        [Description("Date when the cell was created in VisiAdmin.")]
        [ReadOnly(true)]
        public string CreatedDate
        {
            get { return _createdDate; }
            set
            {
                _createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        private string _lastUpdatedBy;
        [Category("3. Update History")]
        [DisplayName("Updated By")]
        [Description("User who last edited the cell in VisiAdmin.")]
        [ReadOnly(true)]
        public string LastUpdatedBy
        {
            get { return _lastUpdatedBy; }
            set
            {
                _lastUpdatedBy = value;
                OnPropertyChanged("LastUpdatedBy");
            }
        }
        
        private string _lastUpdatedDate;
        [Category("3. Update History")]
        [DisplayName("Updated On")]
        [Description("Date when the cell was last edited in VisiAdmin.")]
        [ReadOnly(true)]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        private bool _isEdited;
        public bool IsEdited
        {
            get { return _isEdited; }
            set
            {
                _isEdited = value;
                OnPropertyChanged("IsEdited");
                OnPropertyChanged("IsItalic");
            }
        }

        private bool _isDeleted;
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set
            {
                _isDeleted = value;
                OnPropertyChanged("IsDeleted", true);
                OnPropertyChanged("IsItalic");
            }
        }

        public bool IsItalic
        {
            get { return IsDeleted || IsEdited; }
        }

        private string _status;

        [Category("1. VisiBoard")]
        [DisplayName("Status")]
        [Description("VisiBoard Status.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string Status
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        private string _version;

        [Category("1. VisiBoard")]
        [DisplayName("Version")]
        [Description("VisiBoard Version.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string Version
        {
            get
            {
                return _version;
            }
            set
            {
                _version = value;
                OnPropertyChanged("Version");
            }
        }

        private string _wifi;

        [Category("1. VisiBoard")]
        [DisplayName("WiFi")]
        [Description("WiFi.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string WiFi
        {
            get
            {
                return _wifi;
            }
            set
            {
                _wifi = value;
                OnPropertyChanged("WiFi");
            }
        }


        private string _ipAddress;

        [Category("1. VisiBoard")]
        [DisplayName("Ip Address")]
        [Description("VisiBoard Ip Address.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string IpAddress
        {
            get
            {
                return _ipAddress;
            }
            set
            {
                _ipAddress = value;
                OnPropertyChanged("IpAddress");
            }
        }



        private string _battery;

        [Category("1. VisiBoard")]
        [DisplayName("Battery")]
        [Description("Battery.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string Battery {
            get { return _battery; }
            set { 
                _battery = value;
                OnPropertyChanged("Battery");
            }
        }

        private string _batteryLife;

        [Category("1. VisiBoard")]
        [DisplayName("Battery Life")]
        [Description("Battery Life.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string BatteryLife
        {
            get { return _batteryLife; }
            set
            {
                _batteryLife = value;
                OnPropertyChanged("BatteryLife");
            }
        }

        private string _batteryCharging;

        [Category("1. VisiBoard")]
        [DisplayName("Battery Charging")]
        [Description("Battery Charging.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string BatteryCharging
        {
            get { return _batteryCharging; }
            set
            {
                _batteryCharging = value;
                OnPropertyChanged("BatteryCharging");
            }
        }

        private string _last_reboot;

        [Category("1. VisiBoard")]
        [DisplayName("Last Reboot")]
        [Description("Last reboot.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string LastReboot
        {
            get { return _last_reboot; }
            set
            {
                _last_reboot = value;
                OnPropertyChanged("LastReboot");
            }
        }

        private string _visiBoxIpAddress;

        [Category("2. VisiBox")]
        [DisplayName("Ip Address")]
        [Description("VisiBox Ip Address.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string VisiBoxIpAddress
        {
            get
            {
                return _visiBoxIpAddress;
            }
            set
            {
                _visiBoxIpAddress = value;
                OnPropertyChanged("VisiBoxIpAddress");
            }
        }

        private string _visiBoxStatus;

        [Category("2. VisiBox")]
        [DisplayName("Status")]
        [Description("VisiBox Status.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string VisiBoxStatus
        {
            get
            {
                return _visiBoxStatus;
            }
            set
            {
                _visiBoxStatus = value;
                OnPropertyChanged("VisiBoxStatus");
            }
        }

        private string _visiBoxVersion;

        [Category("2. VisiBox")]
        [DisplayName("Version")]
        [Description("VisiBox Version.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string VisiBoxVersion
        {
            get
            {
                return _visiBoxVersion;
            }
            set
            {
                _visiBoxVersion = value;
                OnPropertyChanged("VisiBoxVersion");
            }
        }

        private string _visibox_wifi;

        [Category("2. VisiBox")]
        [DisplayName("WiFi")]
        [Description("WiFi.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string VisiBoxWiFi
        {
            get
            {
                return _visibox_wifi;
            }
            set
            {
                _visibox_wifi = value;
                OnPropertyChanged("WiFi");
            }
        }


        private string _visibox_last_reboot;

        [Category("2. VisiBox")]
        [DisplayName("Last Reboot")]
        [Description("Last reboot.")]
        [DefaultValue("")]
        [ReadOnly(true)]
        public string VisiBoxLastReboot
        {
            get { return _visibox_last_reboot; }
            set
            {
                _visibox_last_reboot = value;
                OnPropertyChanged("VisiBoxLastReboot");
            }
        }


        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                return string.Empty; ;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(VisiBoardStatusModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(CellName, other.CellName)
                && string.Equals(Description, other.Description)
                && IsDeleted == other.IsDeleted
                && Password == other.Password
                && Enabled == other.Enabled
                && VisiCentralGroup == other.VisiCentralGroup
                && VisiCentralRow == other.VisiCentralRow
                && VisiCentralColumn == other.VisiCentralColumn
                && Status == other.Status
                && Version == other.Version
                && VisiBoxStatus == other.VisiBoxStatus
                && VisiBoxVersion == other.VisiBoxVersion
                && IpAddress == other.IpAddress
                && VisiBoxIpAddress == other.VisiBoxIpAddress;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((VisiBoardStatusModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (CellName ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Description ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Password ?? string.Empty).GetHashCode();
                result = (result * 397) ^ IsDeleted.GetHashCode();
                result = (result * 397) ^ Enabled.GetHashCode();
                return result;
            }
        }
        #endregion
    }

    public class StatusObject
    {
        public string Cell { get; set; }
        public string Status { get; set; }
        public string Version { get; set; }
        public string IpAddress { get; set; }
        public string WiFi { get; set; }
        public string LastReboot { get; set; }
        public string Battery { get; set; }
        public string BatteryLife { get; set; }
        public string BatteryCharging { get; set; }
    }

    public class StatusRequestWrapper
    {
        public int CellId { get; set; }
        public bool IsVisiBoxStatus { get; set; }
        public string RequestUrl { get; set; }
    }

    public class StatusResponseWrapper
    {
        public int CellId { get; set; }
        public bool Success { get; set; }
        public string ErrorMessage { get; set; }
        public bool IsVisiBoxStatus { get; set; }
        public StatusObject ResultObject { get; set; }
    }
}
